package com.fenghuajueli.module_user.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils;
import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo;
import com.fenghuajueli.libbasecoreui.constants.SwitchKeyConstants;
import com.fenghuajueli.libbasecoreui.data.entity.goods.GoodsEntity;
import com.fenghuajueli.libbasecoreui.utils.TimeUtils;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.databinding.DialogVipPayBinding;

public class VipPayDialog extends Dialog {
    private DialogVipPayBinding binding;
    private OnPayWaySelectListener listener = null;
    private String price = null;
    private String background = null;
    private String primaryColor = null;
    private GoodsEntity entity = null;

    public VipPayDialog(@NonNull Context context) {
        super(context, R.style.ResultDialog);
    }

    public VipPayDialog(Context context, String price, OnPayWaySelectListener listener) {
        super(context, R.style.ResultDialog);
        this.price = price;
        this.listener = listener;
    }

    public OnPayWaySelectListener getListener() {
        return listener;
    }

    public void setListener(OnPayWaySelectListener listener) {
        this.listener = listener;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getBackground() {
        return background;
    }

    public void setBackground(String background) {
        this.background = background;
    }

    public String getPrimaryColor() {
        return primaryColor;
    }

    public void setPrimaryColor(String primaryColor) {
        this.primaryColor = primaryColor;
    }

    public GoodsEntity getEntity() {
        return entity;
    }

    public void setEntity(GoodsEntity entity) {
        this.entity = entity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DialogVipPayBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);

        initView();
        payChannelshow();
        initListener();
    }

    private void initView() {
        new CountDownTimer(60 * 60 * 1000, 1000) {

            @Override
            public void onTick(long time) {
                String[] timeValues = TimeUtils.msecToTime(time).split(":");
                binding.tvHour.setText(timeValues[0]);
                binding.tvMinute.setText(timeValues[1]);
                binding.tvSecond.setText(timeValues[2]);
            }

            @Override
            public void onFinish() {
                if (listener != null) {
                    listener.select("pay");
                }
                dismiss();
            }
        }.start();
    }

    String pay = "pay2";

    private void initListener() {
        if (price != null) {
            binding.tvPrice.setText("永久会员 特价¥" + price);
        }
        if (background != null) {
            Glide.with(getContext()).load(background).into(binding.ivBg);
        }
        if (primaryColor != null) {
            int color = Color.parseColor(primaryColor);
            binding.tvHour.setBackgroundTintList(ColorStateList.valueOf(color));
            binding.tvMinute.setBackgroundTintList(ColorStateList.valueOf(color));
            binding.tvSecond.setBackgroundTintList(ColorStateList.valueOf(color));
            binding.btnPay.getBackground().setTint(color);
        }
        binding.btnAliPay.setOnClickListener(v -> {
            binding.btnAliPay.setChecked(true);
            binding.btnWechatPay.setChecked(false);
            pay = "pay1";
        });
        binding.btnWechatPay.setOnClickListener(v -> {
            binding.btnAliPay.setChecked(false);
            binding.btnWechatPay.setChecked(true);
            pay = "pay2";
        });
        binding.btnPay.setOnClickListener(v -> {
            if (listener != null) {
                listener.select(pay);
            }
            dismiss();
        });

        binding.ivDismiss.setOnClickListener(v -> {
            if (listener != null) {
                listener.select("exit");
            }
            dismiss();
        });
    }

    private void payChannelshow() {
        var aliPaySwitch = true;
        var wechatPaySwitch = true;
        switch (SwitchKeyUtils.getPayChannelType()) {
            case 2 -> aliPaySwitch = false;
            case 3 -> wechatPaySwitch = false;
        }

        if (SwitchKeyUtils.getWechatPayType() != SwitchKeyConstants.WECHAT_PAY_TYPE_H5 &&
                (AppConfigInfo.WECHAT_APP_ID.startsWith("xxxx") ||
                        AppConfigInfo.WECHAT_APP_SECRET.startsWith("xxxx") ||
                        TextUtils.isEmpty(AppConfigInfo.WECHAT_APP_ID) ||
                        TextUtils.isEmpty(AppConfigInfo.WECHAT_APP_SECRET))
        ) {
            wechatPaySwitch = false;
        }

        if (wechatPaySwitch && !aliPaySwitch) {
            binding.btnAliPay.setVisibility(View.GONE);
            binding.btnWechatPay.setVisibility(View.VISIBLE);
            binding.btnAliPay.setChecked(false);
            binding.btnWechatPay.setChecked(true);
            pay = "pay2";
        } else if (aliPaySwitch && !wechatPaySwitch) {
            binding.btnAliPay.setVisibility(View.VISIBLE);
            binding.btnWechatPay.setVisibility(View.GONE);
            binding.btnAliPay.setChecked(true);
            binding.btnWechatPay.setChecked(false);
            pay = "pay1";
        } else {
            binding.btnAliPay.setVisibility(View.VISIBLE);
            binding.btnWechatPay.setVisibility(View.VISIBLE);
            binding.btnAliPay.setChecked(false);
            binding.btnWechatPay.setChecked(true);
            pay = "pay2";
        }
    }

    public interface OnPayWaySelectListener {
        void select(String type);
    }
}
